package com.gxa.jjty.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.jjty.admin.service.SystemMsgService;
import com.gxa.jjty.core.common.ExceptionCode;
import com.gxa.jjty.core.exception.BusinessException;
import com.gxa.jjty.core.pojo.AppServe;
import com.gxa.jjty.core.pojo.SystemMsg;
import com.gxa.jjty.core.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author asus
 */
@Service
@Transactional(rollbackFor = Throwable.class)
public class SystemMsgServiceImpl extends BaseServiceImpl<SystemMsg> implements SystemMsgService {

    @Autowired
    Mapper<SystemMsg> SystemMsgMapper;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    ListOperations<String, Object> listOps=null;

    @Override
    public PageInfo<SystemMsg> selectSM(Integer page, Integer size, Date sReleaseTime, Date eReleaseTime, String publisher, String title) {
       PageHelper.startPage(page,size);
        Example example = new Example(SystemMsg.class);
        Example.Criteria criteria = example.createCriteria();
        if (sReleaseTime != null && eReleaseTime !=null ) {
            criteria.andBetween("releaseTime", sReleaseTime, eReleaseTime);
        }
        if (publisher != null && publisher !=""){
            criteria.andLike("publisher","%"+publisher+"%");
        }
        if (title != null && title !=""){
            criteria.andLike("title","%"+title+"%");
        }
        example.orderBy("releaseTime").desc();
        List<SystemMsg> systemMessages = SystemMsgMapper.selectByExample(example);
        PageInfo<SystemMsg> info = PageInfo.of(systemMessages);

        return info;

    }

    @Override
    public PageInfo<SystemMsg> selectMessage(Integer page, Integer size, String title, Integer msgStatus) {
        PageHelper.startPage(page,size);
        Example example = new Example(SystemMsg.class);
        Example.Criteria criteria = example.createCriteria();
        if (title != null && title !="" ) {
            criteria.andLike("title","%"+title+"%");
        }
        if (msgStatus != null ){
            criteria.andEqualTo("messageStatus",msgStatus);
        }

        example.orderBy("releaseTime").desc();
        List<SystemMsg> systemMessages = SystemMsgMapper.selectByExample(example);
        PageInfo<SystemMsg> info = PageInfo.of(systemMessages);

        return info;

    }

    @Override
    public void insertSystemMsg(SystemMsg systemMsg) throws BusinessException {
         listOps = redisTemplate.opsForList();

        int i = SystemMsgMapper.insertSelective(systemMsg);
        if(i != 1){
            throw  new BusinessException(ExceptionCode.ADD_DEFEATED);
        }
        listOps.rightPush("msgList",systemMsg);

        Set<String> keys = redisTemplate.keys("msgList:*");
        if ((keys != null ? keys.size() : 0) >0){
            redisTemplate.delete(keys);
        }
    }
}


